<?php
/**
 * @author Garry
 *
 *
 */
class Dao extends DomainObject {
	public $oid=0;
	/**
	 * 
	 * Construct the DAO
	 * @param unknown_type $first
	 */
	function __construct( $action = FALSE ) {
		$this->mapper = ObjectMapper::GetInstance();
		$this->anonymous = new DomainObjectCollection();
		$this->property_types = array_merge_recursive(
			$this->property_types,
			array(
	                        'id' => array(
	                                                 'type' => 'INT',
	                                                 'constraint' => 6,
	                                                 'unsigned' => TRUE,
	                                                 'auto_increment' => TRUE
	                                          ),
	                        'oid' => array(
	                                                 'type' => 'INT',
	                                                 'constraint' => 6,
	                                                 'unsigned' => TRUE
	                                          ),
                        'childoids' => array(
                                                 'type' => 'TEXT',
                                                 'null' => TRUE,
                                          )
	                     ,
                        'parentobject' => array(
                                                 'type' => 'VARCHAR',
                                                 'constraint' => 12,
	                     						 'null' => TRUE
                                          )       
	                )
			);
		if( is_bool($action) ){
			// get a valid object id 
			$this->oid = $this->mapper->GetID( get_class( $this ) );
			// load each of these properties into the properties array			
			ObjectWatcher::addNew($this);

		}
		elseif( is_object( $action )){
			$this->mapper-> LoadProperties( $this,$action );
		}
		elseif( is_numeric( $action )){
			$this->oid = $action;
			$this->mapper->load( $this );
		}
	}

	/**
	 *
	 * @param  unknown_type $object
	 * @see DomainObject::doAddObject()
	 */
	protected  function doAddObject($object) {
		if( ! $object instanceof DomainObject ){
			throw new InvalidArgumentException("Only objects of type domain object may be added to Domain Objects", 102);
		}
	    if( !$this->anonymous->contains( $object ) ){
	    	$this->anonymous->addElement( $object );
	    }
	    $this->childoids[]= $object->oid;
		$this->changed['childoids'] = implode(",",$this->childoids); 
		$this->markChanged();
		//TODO - Insert your code here
	}	
	/**
	 * load in child objects
	 * @param  unknown_type $object
	 * @see DomainObject::doGetObjects()
	 */
	protected  function doGetObjects() {
		return $this->anonymous;
		//TODO - Insert your code here
	}
	/**
	 * 
	 * 
	 * Lazy loading of child object
	 * @param unknown_type $oid
	 */
	protected function doGetObject( $oid ){
		$object = $this->mapper->loadObject( $oid );
		return $oid;
	}
	/**
	 *
	 * @param  unknown_type $type

	 * @see DomainObject::getMapper()
	 */
	protected  function getMapper($type) {

		return $this->mapper;
	}



	/**
	 *
	 * @see DomainObject::setMapper()
	 */
	public  function setMapper() {

		//TODO - Insert your code here
	}
}


?>